﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class StockTake_StockTakeDetail : PageBase
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null) return "";
            else return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }
    private string _StockTakeID
    {
        get
        {
            if (ViewState["StockTakeID"] == null) return "";
            else return ViewState["StockTakeID"].ToString();
        }
        set { ViewState["StockTakeID"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ViewState["SortOrder"] = "T_STORE.T_PRODUCT_DETAIL.T_PRODUCT.NAME";
            ViewState["OrderDire"] = "Desc";
            //ViewState["SortOrder"] = string.Empty;
            //ViewState["OrderDire"] = string.Empty;            
            action = Request.QueryString["action"];
            if (action == null || action == "" || action.ToString() == "add")
            {
                action = "add";
                lblTitle.Text = "添加盘点单";
                CreateTime.Text = DateTime.Now.ToShortDateString();
                this.SerialNo.Visible = false;
                this.MainSave.Visible = true;
                this.yinyong.Visible = false;
                this.btnReport.Visible = false;

            }
            else if (action == "edit")
            {
                lblTitle.Text = "编辑盘点单";
                _StockTakeID = Request.QueryString["ID"];
                ShowReport.Visible = true;
                MainSave.Visible = false;
                DetailDiv.Visible = true;
                MainDelete.Visible = true;
                MainFinish.Visible = true;
                StockTakeBLL stockTakeBLL = new StockTakeBLL();
                T_STOCKTAKE stockTake = stockTakeBLL.GetStockTakeByStockTakeID(_StockTakeID);
                SearchResult.DataSource = stockTake.T_STOCKTAKE_DETAILs;
                SearchResult.DataBind();

                this.StartTime.Text = stockTake.START_TIME.Value.ToShortDateString();
                this.EndTime.Text = stockTake.END_TIME.Value.ToShortDateString();
                this.CreateTime.Text = stockTake.CREATE_TIME.Value.ToShortDateString();
                this.SerialNo.Text = Convert.ToString(stockTake.SERIAL_NO);
                this.SerialNo.Visible = true;
                this.MainSave.Visible = true;
                this.btnReport.Visible = false;
            }
            else
            {
                lblTitle.Text = "查看盘点单";
                _StockTakeID = Request.QueryString["ID"];
                ShowReport.Visible = true;
                DetailDiv.Visible = false;
                MainDelete.Visible = false;
                MainFinish.Visible = false;
                MainSave.Visible = false;
                this.yinyong.Visible = false;
                StockTakeBLL stockTakeBLL = new StockTakeBLL();
                T_STOCKTAKE stockTake = stockTakeBLL.GetStockTakeByStockTakeID(_StockTakeID);
                SearchResult.DataSource = stockTake.T_STOCKTAKE_DETAILs;
                SearchResult.DataBind();

                this.StartTime.Text = stockTake.START_TIME.Value.ToShortDateString();
                this.EndTime.Text = stockTake.END_TIME.Value.ToShortDateString();
                this.CreateTime.Text = stockTake.CREATE_TIME.Value.ToShortDateString();
                this.SerialNo.Text = Convert.ToString(stockTake.SERIAL_NO);
                this.SerialNo.Visible = true;
                this.SearchResult.Columns[6].Visible = false;
            }

            this.StartTime.Attributes["readonly"] = "readonly";
            this.EndTime.Attributes["readonly"] = "readonly";
        }
        CreateTime.ReadOnly = true;
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string[] IDs = StoreID.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

        foreach (string id in IDs)
        {
            StockTakeDetailBLL stockTakeDetailBLL = new StockTakeDetailBLL();
            T_STOCKTAKE_DETAIL Item = new T_STOCKTAKE_DETAIL();

            Item.STOCKTAKE_ID = new Guid(this._StockTakeID);
            Item.STOREID = new Guid(id);
            stockTakeDetailBLL.AddStockTakeDetail(Item);
        }
        BoundDetails(this._StockTakeID);
    }
    protected void MainSave_Click(object sender, EventArgs e)
    {
        StockTakeBLL stockTakeBLL = new StockTakeBLL();

        if (action == "add" && string.IsNullOrEmpty(_StockTakeID))
        {
            T_STOCKTAKE stockTake = new T_STOCKTAKE();
            string NewStockTakeID = string.Empty;

            stockTake.CREATE_TIME = DateTime.Now;
            stockTake.CREATOR_ID = this.UserInfo.ID;
            stockTake.START_TIME = DateTime.Parse(this.StartTime.Text);
            stockTake.END_TIME = DateTime.Parse(this.EndTime.Text);
            stockTake.STATE = (int)Enumerations.StockStatus.提交审核中;

            stockTakeBLL.AddStockTake(stockTake, out NewStockTakeID);

            _StockTakeID = NewStockTakeID;
            DetailDiv.Visible = true;
            MainDelete.Visible = true;
            MainFinish.Visible = true;
            
            this.yinyong.Visible = true;
            MainSave.Visible = false;
            ShowReport.Visible = true;
        }
        else
        {
            T_STOCKTAKE stockTake = stockTakeBLL.GetStockTakeByStockTakeID(_StockTakeID);

            stockTake.CREATE_TIME = DateTime.Now;
            stockTake.CREATOR_ID = this.UserInfo.ID;
            stockTake.STATE = (int)Enumerations.StockStatus.提交审核中;

            stockTakeBLL.UpdateStockTake(stockTake);
        }

    }
    protected void MainDelete_Click(object sender, EventArgs e)
    {
        new StockTakeBLL().DeleteStockTake(_StockTakeID);
       // CreateTime.Text = string.Empty;
        StartTime.Text = string.Empty;
        SerialNo.Text = string.Empty;
        EndTime.Text = string.Empty;
        Remark.Value = string.Empty;
        DetailDiv.Visible = false;
        MainDelete.Visible = false;
        MainFinish.Visible = false;
        SearchResult.DataSource = null;
        SearchResult.DataBind();
        yinyong.Visible = false;
        _StockTakeID = "";
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        StockTakeDetailBLL stockTakeDetailBLL = new StockTakeDetailBLL();
        T_STOCKTAKE_DETAIL Item = new T_STOCKTAKE_DETAIL();

        Item.STOCKTAKE_ID = new Guid(this._StockTakeID);
        Item.STOREID = new Guid(StoreID.Text.Trim());
        stockTakeDetailBLL.AddStockTakeDetail(Item);
        BoundDetails(this._StockTakeID);
    }
    protected void MainFinish_Click(object sender, EventArgs e)
    {
        if (action == null || action == "" || action.ToString() == "add")
        {
            Response.Redirect("StockTakeDetail.aspx?action=add");
        }
        else
        {
            Response.Redirect("StockTakeManager.aspx");
        }
    }
    protected void SearchResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ID = SearchResult.DataKeys[e.RowIndex]["ID"].ToString();
        StockTakeDetailBLL stockTakeDetailBLL = new StockTakeDetailBLL();
        stockTakeDetailBLL.DeleteStockTakeDetail(ID);
        BoundDetails(this._StockTakeID);
    }
    //protected void MainFinish0_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("StockTakeDetail.aspx");
    //}
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResult.PageIndex = e.NewPageIndex;
        BoundDetails(this._StockTakeID);
    }

    protected void BoundDetails(string ID)
    {
        StockTakeBLL stockTakeBLL = new StockTakeBLL();
        stockTakeBLL.AddOrderByCondition(ViewState["SortOrder"] + " " + ViewState["OrderDire"]);
        SearchResult.DataSource = stockTakeBLL.GetStockTakeDetailByStockTakeID(ID);
        SearchResult.DataBind();
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.ID)) return;
        string js = "<script language='javascript'>window.open(\"../report/ReportView.aspx?type=stocktake&ID=" + _StockTakeID + "\",\"_blank\");</script>";
        Response.Write(js);
    }
    protected void SearchResult_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        if (ViewState["SortOrder"].ToString() == sortExpression)
        {
            if (ViewState["OrderDire"].ToString() == "Desc")
                ViewState["OrderDire"] = "Asc";
            else
                ViewState["OrderDire"] = "Desc";
        }
        else
        {
            ViewState["SortOrder"] = sortExpression;
        }
        BoundDetails(this._StockTakeID);
    }
}
